<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/6/20.
 */

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;
use App\Models\ChannelField;
use App\Service\ChannelFieldService;
use App\Service\ChannelModelService;
use App\Utils\ResultsVo;
use Illuminate\Http\Request;

class ChannelFieldController extends Controller
{
    protected $channelFieldService;
    protected $channelModelService;

    public function __construct(
        ChannelFieldService $channelFieldService,
        ChannelModelService $channelModelService
    ) {
        parent::__construct();
        $this->channelFieldService = $channelFieldService;
        $this->channelModelService = $channelModelService;
    }

    public function index($id): \Illuminate\Http\JsonResponse
    {
        $data = ChannelField::query()->select([
            'id', 'channel_model_id', 'default_val', 'field', 'intro', 'is_release', 'is_required', 'is_search', 'is_table',
            'setup', 'status', 'tips', 'title', 'type', 'weight',
        ])->where('channel_model_id', $id)
            ->orderBy('weight', 'desc')->get()->toArray();

        return ResultsVo::success($data);
    }

    public function store(Request $request, $id): \Illuminate\Http\JsonResponse
    {
        $results = $request->get('results');
        $master = $request->get('main');
        // 更新主表的安装数据

        // 更新模型字段信息
        $this->channelFieldService->store($results, $id);

        return ResultsVo::success();
    }
}
